package com.xywl.escort.view.order.subscription;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.ProjectOrderListResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class SubscriptionOrderPresenter implements SubscriptionOrderContract.Presenter {
    private SubscriptionOrderContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SubscriptionOrderPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(SubscriptionOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadOrder(int pageNum) {
        BaseReq req = new BaseReq();
        req.pageNum = pageNum;
        mApiServer.projectOrderList(req)
                .compose(RxUtil.<ProjectOrderListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ProjectOrderListResp>bindToLife())
                .subscribe(new BaseSubscriber<ProjectOrderListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadOrderFailure();
                    }

                    @Override
                    public void onNext(ProjectOrderListResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadOrderSuccess(resp.data);
                        } else {
                            mView.loadOrderFailure();
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("SubscriptionOrderPresenter").e("unSubscribe");
        mView = null;
    }
}
